//
//  ForgetPasswordViewController.m
//  JackDeve
//
//  Created by apple on 17/5/9.
//  Copyright © 2017年 wkq. All rights reserved.
//

#import "ForgetPasswordViewController.h"

@interface ForgetPasswordViewController ()
{
    ForgetView * v;
    NSDictionary * _dic;
}
@end

@implementation ForgetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     self.view.backgroundColor =[UIColor whiteColor];
    
    //创建一个添加按钮
    UIButton *navLeftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 5, 20, 20)];
    [navLeftBtn setBackgroundImage:[UIImage imageNamed:@"左箭头"] forState:UIControlStateNormal];
    [navLeftBtn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:navLeftBtn];
    v =[[ForgetView alloc]initWithFrame:self.view.bounds];
    self.view =v;
    //button点击方法
     [v.sureBtn addTarget:self action:@selector(makeSureClick) forControlEvents:UIControlEventTouchUpInside];
    
    self.title =@"修改密码";
}

//点击修改
#pragma mark - Action
- (void)makeSureClick {
    
    
    
    if ([v.pwTf2.text isEqualToString:v.pwTf3.text]&&![v.pwTf.text isEqualToString:v.pwTf2.text]) {    // 新密码 == 确认新密码
        
        [[ForgetModel showData]getDataAndBlock:v.pwTf.text NSstring:v.pwTf2.text];
        
        ForgetModel * model =[[ForgetModel alloc]init];
        //回调数据
        model.block=^(NSDictionary * dic) {
            _dic =dic;
            [self LoginHttpFinish];
        };
     
        
    }
    else if ([v.pwTf.text isEqualToString:v.pwTf2.text]){
        
        UIAlertController *alet = [UIAlertController alertControllerWithTitle:@"提示" message:@"新旧密码不可以一样" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *act1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        UIAlertAction *act2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alet addAction:act1];
        [alet addAction:act2];
        [self presentViewController:alet animated:YES completion:nil];
        
    }
    
    
    else {
        UIAlertController *alet = [UIAlertController alertControllerWithTitle:@"提示" message:@"两次密码输入不一样" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *act1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        UIAlertAction *act2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alet addAction:act1];
        [alet addAction:act2];
        [self presentViewController:alet animated:YES completion:nil];
    }
    
    
    
    }


- (void)LoginHttpFinish
{
    if ([[_dic objectForKey:@"errmsg"] isEqualToString:@"need login"]) {
        //请您登陆
        [SVProgressHUD showErrorWithStatus:@"请您登陆" ];
        [SVProgressHUD dismissWithDelay:1];
    }
    else if ([[_dic objectForKey:@"errmsg"] isEqualToString:@"账户密码修改成功"]){
      
        [SVProgressHUD showSuccessWithStatus:@"密码修改成功" ];
        [SVProgressHUD dismissWithDelay:1];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
    }
    else if ([[_dic objectForKey:@"errmsg"] isEqualToString:@"原始密码错误"]){
        
        UIAlertController *alet = [UIAlertController alertControllerWithTitle:@"提示" message:@"原始密码错误" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *act1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        UIAlertAction *act2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alet addAction:act1];
        [alet addAction:act2];
        [self presentViewController:alet animated:YES completion:nil];
        
    }
    else {
        // 修改失败
        [SVProgressHUD showErrorWithStatus:@"修改失败" ];
        [SVProgressHUD dismissWithDelay:1];
    }
}

//返回方法
-(void)click
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

@end
